/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 10:51:05 AM
 * To change this template use File | Settings | File Templates.
 */
import dk.brics.automaton.Automaton;
import java.util.*;
import org.jdom.Element;

// Referenced classes of package dk.brics.dsd:
//            Regexp, Schema, Context

class UnionRegexp extends Regexp
{

    UnionRegexp(Element element, Schema schema)
    {
        contents = parseList(element, schema);
    }

    public Element toXML(Context context)
    {
        Element element = new Element("union", "http://www.brics.dk/DSD/2.0");
        for(Iterator iterator = contents.iterator(); iterator.hasNext(); element.addContent(((Regexp)iterator.next()).toXML(context)));
        return element;
    }

    boolean match(String s, Context context)
    {
        for(Iterator iterator = contents.iterator(); iterator.hasNext();)
        {
            Regexp regexp = (Regexp)iterator.next();
            if(regexp.match(s, context))
                return true;
        }

        return false;
    }

    Automaton toAutomaton(Context context)
    {
        Iterator iterator = contents.iterator();
        if(!iterator.hasNext())
            return Automaton.makeEmpty();
        Automaton automaton = null;
        while(iterator.hasNext())
        {
            Automaton automaton1 = ((Regexp)iterator.next()).toAutomaton(context);
            if(automaton == null)
                automaton = automaton1;
            else
                automaton = automaton.union(automaton1);
        }
        automaton.minimize();
        return automaton;
    }

    boolean isChar(Context context)
    {
        for(Iterator iterator = contents.iterator(); iterator.hasNext();)
            if(!((Regexp)iterator.next()).isChar(context))
                return false;

        return true;
    }

    void addBoolexps(Context context, Set set)
    {
        for(Iterator iterator = contents.iterator(); iterator.hasNext(); ((Regexp)iterator.next()).addBoolexps(context, set));
    }

    void getMentioned(Context context, Set set, List list)
    {
        for(Iterator iterator = contents.iterator(); iterator.hasNext(); ((Regexp)iterator.next()).getMentioned(context, set, list));
    }

    boolean checkUnion(Context context)
    {
        if(contents.size() == 0)
            return false;
        for(Iterator iterator = contents.iterator(); iterator.hasNext();)
        {
            Regexp regexp = (Regexp)iterator.next();
            if(!regexp.checkRepeatUnion(context) && !regexp.checkUnion(context) && !regexp.checkSimple(context))
                return false;
        }

        return true;
    }

    List contents;
}
